package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    public Page<Setmeal> selectByLimit(Integer page, Integer pageSize, String name) {
        //模糊查询
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<Setmeal> sp = setmealMapper.selectPage(setmealPage, sqw);
        //根据setmeal的category_id查询category
        List<Setmeal> setmeals = sp.getRecords();
        //select name from category where id = setmeal.category_id
        for (Setmeal setmeal : setmeals) {
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());

            //根据setmeal的id查询setmeal_dish列表
            //select * from setmeal_dish where setmeal_id = setmeal.getId()
            LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
            setmeal.setSetmealDishes(setmealDishes);
        }

        return sp;
    }

    //回显
    @Override
    public List<Dish> selectByCateGoryId(Long id,String name) {
        //根据菜品分类表(category)的id查找,dish表中的category_id
        //select * from dish where category_id = Category.getId(Integer id)
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(id != null,Dish::getCategoryId,id);
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        List<Dish> dishes = dishMapper.selectList(qw);
        return dishes;
    }

    @Override
    public void add(Setmeal setmeal) {
        //先添加基本的setameal信息表
        setmealMapper.insert(setmeal);
        //从setmeal中拿出菜品信息，放入套餐-菜品表中
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            System.out.println("==========================================================================");
            System.out.println(setmealDish);
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        //查询setmeal基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //从setmeal中得到的主键id,去setmeal_dish中查询菜品返回
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);

        //将得出的菜品放入setmeal 类中的 setmealDishes
        setmeal.setSetmealDishes(setmealDishes);

        return setmeal;
    }

    //修改回显
    @Override
    public void update(Setmeal setmeal) {
        //先存储基本setmeal的信息
        setmealMapper.updateById(setmeal);
        //先删除旧菜品后添加
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(qw);

        //从setmeal中得到菜品信息
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //取出遍历加上setmeal_id,因为setmeal只有在创建后才能有主键id(回显)
        //所以才需要后端手动添加
        for (SetmealDish setmealDish : setmealDishes) {
            //先添加到实体类中再利用遍历一条一条添加到数据库中
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }


    }

    //状态修改，起售，禁售
    @Override
    public void updateStatusSetmeal(Integer status, List<Long> ids) {
        //update setmeal set status = 1 where id in(1,2,3);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);//where in(1,2,3)
        //创建实体类,设置新status
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //修改数据库数据
        setmealMapper.update(setmeal,qw);
    }

    //删除
    @Override
    public void delete(List<Long> ids) {
        //先判断这些ids的status是不是有禁售状态
        //select count(*) from setmeal where id in(1,2,3) and status = 1;
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus,1);
        qw.in(Setmeal::getId,ids);
        Integer integer = setmealMapper.selectCount(qw);
        //判断是否有起售
        if (integer > 0) {
            throw new CustomException("有起售套餐，无法删除");
        }

        //删除套餐
        setmealMapper.deleteBatchIds(ids);
        //删除菜品套餐中间表
        //delect from setmeal_dish where id in(1,2,3);
        LambdaQueryWrapper<SetmealDish> qw1 = new LambdaQueryWrapper<>();
        qw1.in(SetmealDish::getSetmealId,ids);//where id in(1,2,3)
        setmealDishMapper.delete(qw1);
    }

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //select * from setmeal where categoryId = ? and status = ?;
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId,categoryId);
        sqw.eq(Setmeal::getStatus,status);

        List<Setmeal> setmeals = setmealMapper.selectList(sqw);
        return setmeals;
    }

    //套餐详情
    @Override
    public List<Dish> findDishById(Long id) {
        ArrayList<Dish> dishes = new ArrayList<>();
        //根据套餐(setmeal)id,去setmeal_dish表中查找setmeal_id
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //遍历setmealDishes,得到其中的dish_id
        for (SetmealDish setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            //再用dishId查找dish表的主键ID,得到dish对象
            //select * from dish where id = dishId;
            Dish dish = dishMapper.selectById(dishId);
            //从setmealDish中获得copies,封装到dish中
            dish.setCopies(setmealDish.getCopies());
            dishes.add(dish);
        }

        //select * from dish where category_id = id;
       /* LambdaQueryWrapper<Dish> qw2 = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,id);
        setmealMapper.selectList(qw);*/
        return dishes;
    }
}
